package com.hh.form.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.hh.baseframework.util.Check;
import com.hh.form.bean.model.Column;
import com.hh.form.util.FormUtil;

@Service
public class CreateSourceService {

	public Map<String, Object> createCode(String packName, String tableName,
			String formSource) {
		if (Check.isEmpty(packName)) {
			packName = "com.hh.form";
		}
		Map<String, Object> map = new HashMap<String, Object>();
		List<Column> columnList = FormUtil.xtFormToColumn(formSource);
		String javabean = createJavaBean(packName, tableName, columnList);
		String serviceCode = createServiceCode(packName, tableName, columnList);
		String actionCode = createActionCode(packName, tableName, columnList);
		String pageListCode = createPageListCode(packName, tableName,
				columnList);
		String pageEditCode = createPageEditCode(packName, tableName,
				columnList, formSource);
		map.put("pageListCode", pageListCode);
		map.put("pageEditCode", pageEditCode);
		map.put("javaBean", javabean);
		map.put("serviceCode", serviceCode);
		map.put("actionCode", actionCode);
		return map;
	}

	private String createPageEditCode(String packName, String tableName,
			List<Column> columnList, String formSource) {
		String modelName = packName.substring(packName.lastIndexOf(".") + 1);
		StringBuffer stringBuffer = new StringBuffer();

		stringBuffer.append( "Ext.define('" + packName + "."
				+ tableToClassName(tableName) + "."
				+ tableToClassName(tableName) + "Edit', {");
		stringBuffer.append("</br>"
				+ "extend : 'com.hh.global.SimpleFormPanelWindow',");
		stringBuffer.append("</br>" + "action : '" + modelName + "-"
				+ tableToClassName(tableName) + "-',");
		stringBuffer.append("</br>" + "title : '" + tableToClassName(tableName)
				+ "',");
		stringBuffer.append("</br>" + "width : 600,");
		stringBuffer.append("</br>" + "height : 400,");
		stringBuffer.append("</br>" + "constructor : function(config) {");
		stringBuffer.append("</br>" + "this.config = config || {};");
		stringBuffer.append("</br>"
				+ "this.superclass.constructor.call(this, this.config);");
		stringBuffer.append("</br>" + "this.init();");
		stringBuffer.append("</br>" + "},");
		stringBuffer.append("</br>" + "getFormItems : function() {");
		stringBuffer.append("</br>" + "return "
				+ formSource.substring(0, formSource.length() - 1));
		stringBuffer.append("</br>" + ",{name : 'id',hidden : true}");
		stringBuffer.append("</br>" + "]");
		stringBuffer.append("</br>" + "}");
		stringBuffer.append("</br>" + "});");
		return stringBuffer.toString();
	}

	private String createPageListCode(String packName, String tableName,
			List<Column> columnList) {

		String modelName = packName.substring(packName.lastIndexOf(".") + 1);
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append( "Ext.define('" + packName + "."
				+ tableToClassName(tableName) + "."
				+ tableToClassName(tableName) + "List', {");
		stringBuffer.append("</br>"
				+ "extend : 'com.hh.global.SimpleGridPanelWindow',");
		stringBuffer.append("</br>" + "action : '" + modelName + "-"
				+ tableToClassName(tableName) + "-',");
		stringBuffer.append("</br>" + "editPage : '" + packName + "."
				+ tableToClassName(tableName) + "."
				+ tableToClassName(tableName) + "Edit',");
		stringBuffer.append("</br>" + "constructor : function(config) {");
		stringBuffer.append("</br>" + "this.config = config || {};");
		stringBuffer.append("</br>"
				+ "this.superclass.constructor.call(this, this.config);");
		stringBuffer.append("</br>" + "this.init();");
		stringBuffer.append("</br>" + "},");
		stringBuffer.append("</br>" + "getGridColumns : function() {");
		stringBuffer.append("</br>" + "return [");
		List<String> columnNameList = new ArrayList<String>();
		columnNameList.add("id");
		for (Column column : columnList) {
			stringBuffer.append("</br>" + " {	text : '" + column.getNote()
					+ "',	dataIndex : '" + column.getName() + "',flex : 1	");
			if ("TIMESTAMP".equals(column.getType())) {
				stringBuffer
						.append("</br>"
								+ ",renderer : Ext.util.Format.dateRenderer('Y年m月d日 H时i分s秒')");
			}
			stringBuffer.append("</br>" + "},");
			columnNameList.add(column.getName());
		}
		stringBuffer
				.append("</br>"
						+ "{dataIndex : 'id',	flex : 1,hidden : true},this.getOperateGridColumn()");
		stringBuffer.append("</br>" + "];");
		stringBuffer.append("</br>" + "},");

		stringBuffer.append("</br>" + "getStoreFields : function() {	return "
				+ new Gson().toJson(columnNameList) + ";}");
		stringBuffer.append("</br>" + "});");
		return stringBuffer.toString();
	}

	private String createActionCode(String packName, String tableName,
			List<Column> columnList) {
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("package " + packName + ".action;");

		stringBuffer
				.append("</br>"
						+ "import org.springframework.beans.factory.annotation.Autowired;");
		stringBuffer.append("</br>" + "import " + packName + ".bean."
				+ tableToClassName(tableName) + ";");
		stringBuffer.append("</br>" + "import " + packName + ".service.impl."
				+ tableToClassName(tableName) + "Service;");
		stringBuffer.append("</br>"
				+ "import com.hh.system.service.impl.BaseService;");
		stringBuffer.append("</br>"
				+ "import com.hh.system.util.base.BaseServiceAction;");
		stringBuffer.append("</br>" + "@SuppressWarnings(\"serial\")");
		stringBuffer.append("</br>" + "public class Action"
				+ tableToClassName(tableName) + " extends BaseServiceAction< "
				+ tableToClassName(tableName) + " > {");
		stringBuffer.append("</br>" + "@Autowired");
		stringBuffer.append("</br>" + "private " + tableToClassName(tableName)
				+ "Service service;");

		stringBuffer.append("</br>" + "public BaseService< "
				+ tableToClassName(tableName) + " > getService() {");
		stringBuffer.append("</br>" + "	return service;");
		stringBuffer.append("</br>" + "	}");
		stringBuffer.append("</br>" + "}");
		return stringBuffer.toString();
	}

	private String createServiceCode(String packName, String tableName,
			List<Column> columnList) {
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("package " + packName + ".service.impl;");

		stringBuffer.append("</br>"
				+ "import org.springframework.stereotype.Service;");

		stringBuffer.append("</br>" + "import " + packName + ".bean."
				+ tableToClassName(tableName) + ";");
		stringBuffer.append("</br>"
				+ "import com.hh.system.service.impl.BaseService;");
		stringBuffer.append("</br>" + "@Service");
		stringBuffer.append("</br>" + "public class "
				+ tableToClassName(tableName) + "Service extends BaseService< "
				+ tableToClassName(tableName) + " >{");
		stringBuffer.append("</br>" + "}");
		return stringBuffer.toString();
	}

	private String createJavaBean(String packName, String tableName,
			List<Column> columnList) {
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("package " + packName + ".bean;");
		stringBuffer.append("</br>" + "import javax.persistence.Column;");
		stringBuffer.append("</br>" + "import javax.persistence.Entity;");
		stringBuffer.append("</br>" + "import javax.persistence.Lob;");
		stringBuffer.append("</br>" + "import javax.persistence.Table;");
		stringBuffer.append("</br>"
				+ "import com.hh.hibernate.util.base.BaseTwoEntity;");
		stringBuffer.append("</br>" + "@Entity");
		stringBuffer.append("</br>" + "@Table(name = \"" + tableName + "\")");
		stringBuffer.append("</br>" + "public class "
				+ tableToClassName(tableName)
				+ " extends BaseTwoEntity implements java.io.Serializable {");

		for (Column column : columnList) {
			String javaType = FormUtil.dataBaseTypeToJavaType(column.getType());
			stringBuffer.append("</br>" + "private " + javaType + " "
					+ column.getName() + ";");

			if ("CLOB".equals(column.getType())) {
				stringBuffer.append("</br>" + "@Lob");
			} else {
				stringBuffer.append("</br>" + "@Column(length = "
						+ column.getLength() + ")");
			}

			stringBuffer.append("</br>" + "public " + javaType + " get"
					+ column.getName() + "() {");
			stringBuffer.append("</br>" + "return " + column.getName() + ";");
			stringBuffer.append("</br>" + "}");
			stringBuffer.append("</br>" + "public void set" + column.getName()
					+ "(" + javaType + " " + column.getName() + ") {");
			stringBuffer.append("</br>" + "this." + column.getName() + " = "
					+ column.getName() + ";");
			stringBuffer.append("</br>" + "}");
		}

		stringBuffer.append("</br>" + "}");
		return stringBuffer.toString();
	}

	public String tableToClassName(String tablename) {
		String[] tmp = tablename.split("_");
		// 最终的表名
		String finalClassName = "";
		String firstChar = "";
		String otherChar = "";
		if (tmp.length == 1) {
			// 没有下划线
			firstChar = tmp[0].substring(0, 1).toUpperCase();
			otherChar = tmp[0].substring(1).toLowerCase();
			finalClassName = firstChar + otherChar;
		} else {
			for (int i = 0; i < tmp.length; i++) {
				firstChar = tmp[i].substring(0, 1).toUpperCase();
				otherChar = tmp[i].substring(1).toLowerCase();
				finalClassName = finalClassName + firstChar + otherChar;
			}
		}
		return finalClassName;
	}
}
